 
package com.ld.hpfs.service.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;



import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.ld.hpfs.dao.CompanyMapper;
import com.ld.hpfs.pojo.CompanyDetail;
import com.ld.hpfs.pojo.Company;
import com.ld.hpfs.service.CompanyService;

@Service
public class CompanyServiceImpl implements CompanyService {
	private static final Logger logger = LoggerFactory.getLogger(CompanyServiceImpl.class);
	
	@Autowired
	private CompanyMapper companyMapper;
	
	public int addCompany(CompanyDetail company)
	{
		java.util.HashMap<String, Object> params = new java.util.HashMap<String,Object>();
		params.put("creatorid", company.getCreatorid());
		params.put("createtime", company.getCreatetime());
		params.put("parentid", company.getPid());	
		
		params.put("companycode", company.getCompanycode());
		params.put("companyname", company.getCompanyname());
		params.put("companyaddress", company.getCompanyaddress());	
		params.put("companycity", company.getCompanycity());
		params.put("companypostcode", company.getCompanypostcode());
		params.put("accountingrule", company.getCompanyrule());	
		params.put("orgcode", company.getCompanyorgcode());
		params.put("accountmanager", company.getAccountmanager());
		params.put("operater", company.getOperator());	
		params.put("checker", company.getChecker());
		params.put("groupid", company.getGroupId());
		params.put("hasledger", company.getHasledger());	
		params.put("archiveInvoice", company.getArchiveInvoice());
		params.put("applyExpress", company.getApplyExpress());
		params.put("cpn_id", company.getCpn_id());
	//	params.put("ignore_balance_chk",company.getIgnoreBalanceChk());
		return companyMapper.addCompany(params);
	}
	
 public int checkCompanyName(CompanyDetail company){
	 java.util.HashMap<String, Object> params = new java.util.HashMap<String,Object>();
	 params.put("companycode", company.getCompanycode());
	 params.put("companyname", company.getCompanyname());
	 
	 return companyMapper.checkCompanyName(params);
    }
	
	 
	
    public ArrayList<Company> getCompanies(HashMap<String,Object> inputparam)
    { 	   	
    	int count = companyMapper.getListCount(inputparam);
    	ArrayList<Company> results = new ArrayList<Company>();
		if(count == 0){
			return null;
		}else
		{
			inputparam.put("pageCount", count);
			ArrayList<HashMap<String,Object>> companies = companyMapper.getCompanies(inputparam);
				
		    int rootPid = 0;	
		    Company currentCompany = null;
			for (int i = 0; i < companies.size(); i++) {
				HashMap<String, Object> param = companies.get(i);
				Company company = new Company();
				company.setId(Integer.parseInt(param.get("id").toString()));
				company.setPid(Integer.parseInt(param.get("pid").toString()));
				company.setCompanyname(param.get("companyname").toString());

				if (company.getPid() == rootPid) {
					results.add(company);				
					currentCompany = company;
					 findChildren(currentCompany,companies);									
				}
				/*else
				{
					if (currentCompany!=null && currentCompany.getId() == company.getPid())
					{
						currentCompany.getChildren().add(company);
					}else
					{
						currentCompany = findParentcompany(results,company.getPid());
						if (currentCompany != null)
						{
							currentCompany.getChildren().add(company);
						}
					}
				}*/
			}
		}
    	
    	return results;
    }
    
    private void findChildren(Company currentCompany,ArrayList<HashMap<String,Object>> companies){
    	for(int j=0;j < companies.size();j++){
			HashMap<String, Object> param1 = companies.get(j);			
			Company company1 = new Company();
			company1.setId(Integer.parseInt(param1.get("id").toString()));
			company1.setPid(Integer.parseInt(param1.get("pid").toString()));
			company1.setCompanyname(param1.get("companyname").toString());			
			if((currentCompany.getId()!=company1.getId()) && (company1.getPid()!= currentCompany.getPid()) && (company1.getPid()!= 0)){//排除自身，兄弟节点和根节点
			if(currentCompany.getId() == company1.getPid()){
			     currentCompany.getChildren().add(company1);			    
			     findChildren(company1,companies);
			     }
			}
		}
    }
    private Company findParentcompany(List<Company> companies, int id)
    {
    	for(int i=0; i<companies.size(); i++)
    	{
    		int cid = companies.get(i).getId();
    		if (id == cid) 
    			return companies.get(i);
    		else
    		{
    			List<Company> children = companies.get(i).getChildren();
    			if (children != null && children.size() > 0)
    			{
    				Company c = findParentcompany(children, id);
    				if (c != null) return c;
    			}
    		}
    	}
    	  	  	
    	return null;
    }
    
    public CompanyDetail getCompany(int id)
    {
    	ArrayList<HashMap<String,Object>> companies = companyMapper.getCompany(id);
    	if (companies != null && companies.size()>0)
    	{
    		HashMap<String,Object> hash = companies.get(0);
    		CompanyDetail detail = new CompanyDetail();
    		
    		detail.setCompanyId(Integer.parseInt(hash.get("id").toString()));
    		detail.setPid(Integer.parseInt(hash.get("pid").toString()));
    		detail.setCompanycode(hash.get("companycode").toString());
    		detail.setCompanyname(hash.get("companyname").toString());
    		detail.setCompanyaddress(hash.get("companyaddress").toString());
    		detail.setCompanycity(hash.get("companycity").toString());
    		detail.setCompanypostcode(hash.get("companypostcode").toString());
    		detail.setCompanyrule(hash.get("accountingrule").toString());
    		detail.setCompanyorgcode(hash.get("orgcode").toString());
    		detail.setAccountmanager(hash.get("accountmanager").toString());
    		detail.setOperator(hash.get("operater").toString());
    		detail.setChecker(hash.get("checker").toString());
    		detail.setHasledger(hash.get("hasledger").toString());
    		detail.setArchiveInvoice(hash.get("archiveInvoice").toString());
    		detail.setApplyExpress(hash.get("applyExpress").toString());
    		detail.setCpn_id(Integer.parseInt(hash.get("cpn_id").toString()));
    //		detail.setIgnoreBalanceChk(hash.get("ignore_balance_chk").toString());
    		
    		// 一级、二级红废审核者
    		HashMap<String, Object> param = new HashMap<String, Object>();
    		param.put("cid", id);
    		param.put("role", 1);
    		ArrayList<HashMap<String, Object>> first = companyMapper.getCheckers(param);
    		String firstcheckers = "";
    		for(HashMap<String, Object> checker : first){
    			firstcheckers = firstcheckers + checker.get("uid").toString() + ",";
    		}
    		if(firstcheckers.length() > 0){
    			firstcheckers = firstcheckers.substring(0, firstcheckers.length() - 1);
    		}
    		detail.setFirstchecker(firstcheckers);
    		param.put("role", 2);
    		ArrayList<HashMap<String, Object>> second = companyMapper.getCheckers(param);
    		String secondcheckers = "";
    		for(HashMap<String, Object> checker : second){
    			secondcheckers = secondcheckers + checker.get("uid").toString() + ",";
    		}
    		if(secondcheckers.length() > 0){
    			secondcheckers = secondcheckers.substring(0, secondcheckers.length() - 1);
    		}
    		detail.setSecondchecker(secondcheckers);
    		
    		return detail;
    	}
    	
    	return null;
    }
    
    public int deleteCompany(int id)
    {
    	return companyMapper.deleteCompany(id);
    }
    
    public int updateCompany(CompanyDetail company)
    {
    	java.util.HashMap<String, Object> params = new java.util.HashMap<String,Object>();
		params.put("companycode", company.getCompanycode());
		params.put("companyname", company.getCompanyname());
		params.put("companyaddress", company.getCompanyaddress());	
		params.put("companycity", company.getCompanycity());
		params.put("companypostcode", company.getCompanypostcode());
		params.put("accountingrule", company.getCompanyrule());	
		params.put("orgcode", company.getCompanyorgcode());
		params.put("accountmanager", company.getAccountmanager());
		params.put("operater", company.getOperator());	
		params.put("checker", company.getChecker());
		params.put("groupid", company.getGroupId());
		params.put("hasledger", company.getHasledger());
		params.put("archiveInvoice", company.getArchiveInvoice());
		params.put("applyExpress", company.getApplyExpress());
		params.put("cpn_id", company.getCpn_id());
		params.put("id", company.getCompanyId());
		params.put("parentid", company.getPid());
	//	params.put("ignore_balance_chk", company.getIgnoreBalanceChk());
		return companyMapper.updateCompany(params);
    }
    
    public ArrayList<HashMap<String,Object>> getParentCompanyList(HashMap<String,Object> param){
    	return companyMapper.getParentCompanyList(param);
    }
    
    public int checkIsParent(int id){
    	return companyMapper.checkIsParent(id);
    }
    
    public int checkHaveData(int id){
    	return companyMapper.checkHaveData(id);
    }
    
    public int getCompanyNum(){
    	return companyMapper.getCompanyNum();
    }

	@Override
	public ArrayList<HashMap<String, Object>> getUserList(
			HashMap<String, Object> param) {
		return companyMapper.getUserList(param);
	}

	@Override
	public void updateChecker(HashMap<String, Object> param) {
		companyMapper.deleteCheckers(param);
		if(param.containsKey("firstchecker")){
			String first = param.get("firstchecker").toString();
			if(first.length() > 0){
				String[] firstCheckers = first.split(",");
				for(int i = 0; i < firstCheckers.length; i++){
					HashMap<String, Object> add = new HashMap<String, Object>();
					add.put("createid", param.get("createid").toString());
					add.put("cid", Integer.parseInt(param.get("cid").toString()));
					add.put("role", 1);
					add.put("uid", Integer.parseInt(firstCheckers[i]));
					System.out.println(Integer.parseInt(firstCheckers[i]));
					companyMapper.addChecker(add);
				}
			}
		}
		if(param.containsKey("secondchecker")){
			String second = param.get("secondchecker").toString();
			if(second.length() > 0){
				String[] secondCheckers = second.split(",");
				for(int i = 0; i < secondCheckers.length; i++){
					HashMap<String, Object> add = new HashMap<String, Object>();
					add.put("createid", param.get("createid").toString());
					add.put("cid", Integer.parseInt(param.get("cid").toString()));
					add.put("role", 2);
					add.put("uid", Integer.parseInt(secondCheckers[i]));
					System.out.println(Integer.parseInt(secondCheckers[i]));
					companyMapper.addChecker(add);
				}
			}
		}
		
		// 维护人员角色中的“红废审核者”
		companyMapper.deleteCheckerRoles();
		ArrayList<HashMap<String, Object>> checkers = companyMapper.getCheckers(new HashMap<String, Object>());
		for(HashMap<String, Object> checker : checkers){
			checker.put("createid", param.get("createid").toString());
			companyMapper.addCheckerRole(checker);
		}
	}

    /*
     * 检查是否启用发票归档功能
     */
	@Override
	public int getArchiveInvoice(HashMap<String, Object> param) {
		return companyMapper.getArchiveInvoice(param);
	}
	/*
     * 检查是否启用发票归档功能
     */
	@Override
	public int isExistCompanyCode(HashMap<String, Object> param) {
		return companyMapper.isExistCompanyCode(param);
	}
	
	@Override
	public HashMap<String, Object> getCurrentCompanyCode(
			HashMap<String, Object> param) {
		return companyMapper.getCurrentCompanyCode(param);
	}
}
